package cn.itcast.jobs;

import cn.itcast.constant.RedisConstant;
import cn.itcast.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.JedisPool;

import java.util.Iterator;
import java.util.Set;

/**
 * @version V1.0
 * @author: luoqianjin
 * @date: 2019/7/29 20:53
 * @description: 定时任务：清理垃圾图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    //清理图片
    public void clearImg(){
        //计算redis中两个集合的差值，获取垃圾图片名称
        Set<String> set = jedisPool.getResource().sdiff(
                RedisConstant.SETMEAL_PIC_RESOURCES,
                RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        /*Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()){
            String pic = iterator.next();
            //删除图片服务器中的图片文件
            QiniuUtils.deleteFileFromQiniu(pic);
            //删除redis中的数据
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,pic);
         }*/
        if (!CollectionUtils.isEmpty(set)) {
            set.forEach(item->{
                QiniuUtils.deleteFileFromQiniu(item);
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,item);
                System.out.println("删除图片"+item);
            });
        }
        }
    }
